package org.dydl.service.system.impl;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.IGoodsRepository;
import org.dydl.dao.system.repository.IMaterialsPurchaseDetailRepository;
import org.dydl.dao.system.repository.IMaterialsPurchaseRepository;
import org.dydl.model.system.MaterialsPurchase;
import org.dydl.model.system.MaterialsPurchaseDetail;
import org.dydl.model.system.vo.PurchaseOrderVo;
import org.dydl.service.system.MaterialsPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("materialsPurchaseService")
public class MaterialsPurchaseServiceImpl implements MaterialsPurchaseService{
	@Autowired
	private IMaterialsPurchaseRepository iMaterialsPurchaseRepository;
	
	@Autowired
	private IMaterialsPurchaseDetailRepository iMaterialsPurchaseDetailRepository;

	@Autowired
	private IGoodsRepository iGoodsRepository;

	@Override
	public ResponseMessage list(int page, int rows, String purchaseSingleNo) {
		
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<MaterialsPurchase> pageList = null;
		if (StringUtils.isNotBlank(purchaseSingleNo)) {
			pageList = iMaterialsPurchaseRepository.getListLike(String.format("%s%s%s", "%", StringUtils.trim(purchaseSingleNo), "%"),
					pageable);
		} else {
			pageList = iMaterialsPurchaseRepository.getList(pageable);
		}

		ResponseMessage responseMessage = new ResponseMessage(true, new ResponseData<MaterialsPurchase>(page, pageList.getTotalPages(),
				((int) pageList.getTotalElements()), pageList.getContent()));

		return responseMessage;
		
	}
	
	@Override
	public ResponseMessage save(MaterialsPurchase materialsPurchase) {
		
		return new ResponseMessage(true, iMaterialsPurchaseRepository.save(materialsPurchase));
	}
	
	@Override
	public void delete(int id) {
		iMaterialsPurchaseRepository.delete(id);
		
	}

	@Override
	public MaterialsPurchase findByPurchaseSingleNo(String purchaseSingleNo) {
		
		
		return iMaterialsPurchaseRepository.findByPurchaseSingleNo(purchaseSingleNo);
	}

	@Override
	public List<MaterialsPurchaseDetail> findDetailByPurchaseSingleNo(String purchaseSingleNo) {
		return iMaterialsPurchaseDetailRepository.findDetailByPurchaseSingleNo(purchaseSingleNo);
	}

	@Override
	public void saveDetail(List<MaterialsPurchaseDetail> grid_arr) {
		for (MaterialsPurchaseDetail MaterialsPurchaseDetail : grid_arr) {
			iMaterialsPurchaseDetailRepository.save(MaterialsPurchaseDetail);
		}
	}

	@Override
	@Transactional
	public ResponseMessage savePurchaseOrder(PurchaseOrderVo purchaseOrderVo) {
		ResponseMessage responseMessage = new ResponseMessage();
		try {
			MaterialsPurchase materialsPurchase = purchaseOrderVo.getMaterialsPurchase();
			List<MaterialsPurchaseDetail> materialsPurchaseDetailList = purchaseOrderVo.getMaterialsPurchaseDetailList();
			iMaterialsPurchaseDetailRepository.deleteDetail(materialsPurchase.getPurchaseSingleNo());
			materialsPurchase.setCreateBy("创建人");
			iMaterialsPurchaseRepository.save(materialsPurchase);
			iMaterialsPurchaseDetailRepository.save(materialsPurchaseDetailList);
			responseMessage.setSuccess(true);
		}catch (Exception e){
			responseMessage.setSuccess(false);
			responseMessage.setMsg(e.getMessage());
		}
			return responseMessage;
	}

	@Override
	public void deleteDetail(String purchaseSingleNo) {
		iMaterialsPurchaseDetailRepository.deleteDetail(purchaseSingleNo);
		
	}

	@Override
	@Transactional
	public void changeExamine(Integer id,Integer status) {
		iMaterialsPurchaseRepository.changeExamine(id,status);
		
	}

	@Override
	@Transactional
	public ResponseMessage affirmMaterisals(String[] purchaseSingleNos) {
		ResponseMessage responseMessage = new ResponseMessage();
		try {
			for (String el :purchaseSingleNos){
				int i =iMaterialsPurchaseRepository.updateMaterialsPurchaseStatus(20,el);
				if(i == 0){
					responseMessage.setSuccess(false);
					responseMessage.setStatus(60001);
					responseMessage.setMsg("未知原因修改状态失败!");
					return responseMessage;
				}else {
					List<MaterialsPurchaseDetail> materialsPurchaseDetailList = iMaterialsPurchaseDetailRepository.findDetailByPurchaseSingleNo(el);
					for (MaterialsPurchaseDetail materialsPurchaseDetail:materialsPurchaseDetailList) {
						int number =iGoodsRepository.updateGoodsStockQtyByGoodsUUID(materialsPurchaseDetail.getTotalQuantity(),materialsPurchaseDetail.getGoodsUuid());
							if (number == 0){
								responseMessage.setSuccess(false);
								responseMessage.setStatus(60002);
								responseMessage.setMsg("未知原因修改商品库存失败!");
								return responseMessage;
							}
						}
				}
			}
			responseMessage.setSuccess(true);
			responseMessage.setMsg("保存成功!");
		}catch (Exception e){
			responseMessage.setSuccess(false);
			responseMessage.setMsg(e.getMessage());
		}
			return responseMessage;
	}


}
